package com.termux.app;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;

import java.io.File;

public class PluginResultsService extends IntentService {

    public static final String EXTRA_EXECUTION_ID = "execution_id";

    private static int EXECUTION_ID = 1000;

    public static final String PLUGIN_SERVICE_LABEL = "PluginResultsService";

    private static final String LOG_TAG = "PluginResultsService";

    public PluginResultsService(){
        super(PLUGIN_SERVICE_LABEL);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent == null) return;

        Log.d(LOG_TAG, PLUGIN_SERVICE_LABEL + " received execution result");

        final Bundle resultBundle = intent.getBundleExtra(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE);
        if (resultBundle == null) {
            Log.e(LOG_TAG, "The intent does not contain the result bundle at the \"" + TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE + "\" key.");
            return;
        }

        final int executionId = intent.getIntExtra(EXTRA_EXECUTION_ID, 0);

        Log.d(LOG_TAG, "Execution id " + executionId + " result:\n" +
            "stdout:\n```\n" + resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT, "") + "\n```\n" +
            "stdout_original_length: `" + resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT_ORIGINAL_LENGTH) + "`\n" +
            "stderr:\n```\n" + resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR, "") + "\n```\n" +
            "stderr_original_length: `" + resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR_ORIGINAL_LENGTH) + "`\n" +
            "exitCode: `" + resultBundle.getInt(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_EXIT_CODE) + "`\n" +
            "errCode: `" + resultBundle.getInt(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_ERR) + "`\n" +
            "errmsg: `" + resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_ERRMSG, "") + "`");

        String mode = intent.getStringExtra("mode");
        String path = intent.getStringExtra("path");
        if (mode.equals("dec")){
//            TermuxActivity.runVVCService(path, PluginResultsService.this, "play");
            // 播放视频并删除
            Intent mediaIntent = new Intent(PluginResultsService.this, MediaActivity.class);
            mediaIntent.putExtra("path", path.replaceAll("\\.(?<=\\.)[^.]+$", VvcCommandService.DEC_0));
            mediaIntent.putExtra("mode", mode);
//            File file = new File(path.replaceAll("\\.(?<=\\.)[^.]+$", VvcCommandService.DEC_0));
//            Uri uri = Uri.fromFile(file);
//            mediaIntent.putExtra("uri", uri.toString());
            mediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(mediaIntent);
        }
        else if (mode.equals("enc")) {
            TermuxActivity.runVVCService(path, PluginResultsService.this, "enc_vvc");
        }
        else if (mode.equals("enc_vvc")) {
            File file = new File(path.replaceAll("\\.(?<=\\.)[^.]+$", VvcCommandService.ENC_0));
            if (file.exists()) {
                file.delete();
            }
//            Toast.makeText(PluginResultsService.this, "保存在"+
//                    path.replaceAll("\\.(?<=\\.)[^.]+$", "_tmp.vvc"),
//                Toast.LENGTH_LONG).show();
            TermuxActivity.runVVCService(path, PluginResultsService.this, "enc_vvc_mp4");
        }
        else if (mode.equals("enc_vvc_mp4")) {
            File file = new File(path.replaceAll("\\.(?<=\\.)[^.]+$", VvcCommandService.ENC_1));
            if (file.exists()) {
                file.delete();
            }
//            Toast.makeText(PluginResultsService.this, "保存在"+
//                path.replaceAll("\\.(?<=\\.)[^.]+$", "_vvc.mp4"),
//                Toast.LENGTH_LONG).show();
        }
    }

    public static synchronized int getNextExecutionId() {
        return EXECUTION_ID++;
    }

}
